{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  to find HSV value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import time\n",
    "# A required callback method that goes into the trackbar function.\n",
    "def nothing(x):\n",
    "    pass\n",
    "\n",
    "# Initializing the webcam feed.\n",
    "cap = cv2.VideoCapture(0)\n",
    "cap.set(3,1280)\n",
    "cap.set(4,720)\n",
    "\n",
    "# Create a window named trackbars.\n",
    "cv2.namedWindow(\"Trackbars\")\n",
    "\n",
    "# Now create 6 trackbars that will control the lower and upper range of \n",
    "# H,S and V channels. The Arguments are like this: Name of trackbar, \n",
    "# window name, range,callback function. For Hue the range is 0-179 and\n",
    "# for S,V its 0-255.\n",
    "cv2.createTrackbar(\"L - H\", \"Trackbars\", 0, 179, nothing)\n",
    "cv2.createTrackbar(\"L - S\", \"Trackbars\", 0, 255, nothing)\n",
    "cv2.createTrackbar(\"L - V\", \"Trackbars\", 0, 255, nothing)\n",
    "cv2.createTrackbar(\"U - H\", \"Trackbars\", 179, 179, nothing)\n",
    "cv2.createTrackbar(\"U - S\", \"Trackbars\", 255, 255, nothing)\n",
    "cv2.createTrackbar(\"U - V\", \"Trackbars\", 255, 255, nothing)\n",
    " \n",
    "while True:\n",
    "    \n",
    "    # Start reading the webcam feed frame by frame.\n",
    "    ret, frame = cap.read()\n",
    "    if not ret:\n",
    "        break\n",
    "    # Flip the frame horizontally (Not required)\n",
    "    frame = cv2.flip( frame, 1 ) \n",
    "    \n",
    "    # Convert the BGR image to HSV image.\n",
    "    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n",
    "    \n",
    "    # Get the new values of the trackbar in real time as the user changes \n",
    "    # them\n",
    "    l_h = cv2.getTrackbarPos(\"L - H\", \"Trackbars\")\n",
    "    l_s = cv2.getTrackbarPos(\"L - S\", \"Trackbars\")\n",
    "    l_v = cv2.getTrackbarPos(\"L - V\", \"Trackbars\")\n",
    "    u_h = cv2.getTrackbarPos(\"U - H\", \"Trackbars\")\n",
    "    u_s = cv2.getTrackbarPos(\"U - S\", \"Trackbars\")\n",
    "    u_v = cv2.getTrackbarPos(\"U - V\", \"Trackbars\")\n",
    " \n",
    "    # Set the lower and upper HSV range according to the value selected\n",
    "    # by the trackbar\n",
    "    lower_range = np.array([l_h, l_s, l_v])\n",
    "    upper_range = np.array([u_h, u_s, u_v])\n",
    "    \n",
    "    # Filter the image and get the binary mask, where white represents \n",
    "    # your target color\n",
    "    mask = cv2.inRange(hsv, lower_range, upper_range)\n",
    " \n",
    "    # You can also visualize the real part of the target color (Optional)\n",
    "    res = cv2.bitwise_and(frame, frame, mask=mask)\n",
    "    \n",
    "    # Converting the binary mask to 3 channel image, this is just so \n",
    "    # we can stack it with the others\n",
    "    mask_3 = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)\n",
    "    \n",
    "    # stack the mask, orginal frame and the filtered result\n",
    "    stacked = np.hstack((mask_3,frame,res))\n",
    "    \n",
    "    # Show this stacked frame at 40% of the size.\n",
    "    cv2.imshow('Trackbars',cv2.resize(stacked,None,fx=0.4,fy=0.4))\n",
    "    \n",
    "    # If the user presses ESC then exit the program\n",
    "    key = cv2.waitKey(1)\n",
    "    if key == 27:\n",
    "        break\n",
    "    \n",
    "    # If the user presses `s` then print this array.\n",
    "    if key == ord('s'):\n",
    "        \n",
    "        thearray = [[l_h,l_s,l_v],[u_h, u_s, u_v]]\n",
    "        print(thearray)\n",
    "        \n",
    "        # Also save this array as penval.npy\n",
    "        np.save('hsv values',thearray)\n",
    "        break\n",
    "    \n",
    "# Release the camera & destroy the windows.    \n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
